package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.util.UserContext;
import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.mapper.AnswerCommentMapper;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IAnswerService;
import cn.wolfcode.trip.base.service.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AnswerServiceImpl implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerCommentMapper answerCommentMapper;

    @Autowired
    private QuestionMapper questionMapper;

    private final static Pattern ATTR_PATTERN = Pattern.compile("<img[^<>]*?\\ssrc=['\"]?(.*?)['\"]?\\s.*?>",Pattern.CASE_INSENSITIVE);

    @Override
    public PageInfo<Answer> query(Long id, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        PageHelper.orderBy(qo.getOrderBy());
        List<Answer> list = answerMapper.selectForList(id, qo);
        for (Answer answer : list) {
            answer.setCommentCount(answerCommentMapper.queryForCountById(answer.getId()));
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Answer> queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        PageHelper.orderBy("question_id , createTime");
        List<Answer> list = answerMapper.selectAll(qo);
        for (Answer answer : list) {
            answer.setCommentCount(answerCommentMapper.queryForCountById(answer.getId()));
        }
        return new PageInfo<>(list);
    }

    @Override
    public void save(Answer answer) {
        String s = answer.getContent();
        String coverUrl = "";
        if(StringUtils.hasText(s)){
            Matcher matcher = ATTR_PATTERN.matcher(s);
            while (matcher.find()) {
                coverUrl += matcher.group(1) + ",";
            }
        }
        if(answer.getId()==null){
            //新增
            answer.setUser(UserContext.getCurrentUser());
            answer.setCreateTime(new Date());
            answer.setGolden(Boolean.FALSE);
            answer.setLikes(0);
            answer.setFavs(0);
            answerMapper.insert(answer);
        }
        Question question = questionMapper.selectByPrimaryKey(answer.getQuestion().getId());
        if (question.getCoverUrl() == null) {
            question.setCoverUrl(coverUrl.split(",")[0]);
            questionMapper.setCoverUrl(question);
        }
    }

    @Override
    public Answer getById(Long id) {
        Answer answer = answerMapper.selectByPrimaryKey(id);
        answer.setCommentCount(answerCommentMapper.queryForCountById(answer.getId()));
        return answer;
    }

    // 获取评论数
    @Override
    public int queryForCommentCountById(Long id) {
        return answerCommentMapper.queryForCountById(id);
    }

    @Override
    public int getAnswerCountByQuestionId(Long id) {
        return answerMapper.getAnswerCountByQuestionId(id);
    }

    @Override
    public void delete(Long id) {
        answerMapper.deleteByPrimaryKey(id);
    }

    // 取消顶
    @Override
    public void delLike(Long uid, Long aid) {
        answerMapper.delLike(aid);
        answerMapper.remAnswerLike(uid, aid);
    }

    // 顶
    @Override
    public void addLike(Long uid, Long aid) {
        answerMapper.addLike(aid);
        answerMapper.insAnswerLike(uid, aid);
    }

    // 查询问题是否被某用户顶
    @Override
    public Answer getLikeAnswerById(Long uid, Long aid) {
        return answerMapper.getLikeAnswerById(uid, aid);
    }

    // 取消收藏
    @Override
    public void delFav(Long uid, Long aid) {
        answerMapper.delFav(aid);
        answerMapper.remAnswerFav(uid, aid);
    }

    // 收藏
    @Override
    public void addFav(Long uid, Long aid) {
        answerMapper.addFav(aid);
        answerMapper.insAnswerFav(uid, aid);
    }

    // 查询问题是否被某用户收藏
    @Override
    public Answer getFavAnswerById(Long uid, Long aid) {
        return answerMapper.getFavAnswerById(uid, aid);
    }

    // 更新金牌状态
    @Override
    public void update(Answer answer) {
        answerMapper.updateByPrimaryKey(answer);
    }

    // 获取某问题下所有回答评论总数
    @Override
    public int getCommentCountByQuestionId(Long id) {
        return answerMapper.getCommentCountByQuestionId(id);
    }
}
